package com._58city.spark.app.redis;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.util.SafeEncoder;

import com._58city.spark.app.ext.JsonUtil;
import com.bj58.ecdata.redis.JedisX;

/**
 * @author lihao
 * Redis的工具类
 */
public class RedisUtil {
	
	private static Logger log = LoggerFactory.getLogger(RedisUtil.class);
	public static JedisX cache;
	public static JedisX cookie_redis ;
	
	static{
//		JedisPoolConfig config = new JedisPoolConfig();
//		config.setMaxActive(500); //如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
//		config.setMaxIdle(100);  //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
//		config.setMaxWait(5000); //表示当borrow(引入)一个jedis实例时，最大的等待时间，如果超过等待时间，则直接抛出JedisConnectionException
//		config.setTestOnBorrow(false); // //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
//		JedisBean bean = new JedisBean();
//		bean.setMasterAddress("10.126.86.167:6379");//新机房线上正式环境
////		bean.setMasterAddress("10.9.19.123:6379");//测试环境
//		cache = new JedisX(bean, config, 7000);
		
		AbstractApplicationContext context =  new ClassPathXmlApplicationContext("application-context.xml");
		cache = context.getBean("cache", JedisX.class);
		cookie_redis = context.getBean("cookie_cache", JedisX.class);
	}
	
    public static <T> void send(String key,Collection<T> collection){
		ShardedJedisPool pool = cache.getShardedJedisPool();
		ShardedJedis shardJedis = null;	
		Jedis jedis = null;	 
		try {
			shardJedis = pool.getResource();
			jedis = shardJedis.getShard(key);
			if (jedis != null) {
				byte[][] rsList = new byte[collection.size()][];
				byte[] bytekey = SafeEncoder.encode(key);
				int i=0;
				for(T t : collection){
					byte[] bytevalue = SafeEncoder.encode(JsonUtil.toJson(t));
					rsList[i++] = bytevalue;
//					jedis.lpush(bytekey, bytevalue);
				}
				jedis.lpush(bytekey, rsList);
			}
		} catch (Exception e) {
			if (shardJedis != null) pool.returnBrokenResource(shardJedis);
			log.error(e.getMessage(), e);
		}finally{
			if (shardJedis != null) pool.returnResource(shardJedis);
		}
	}
    
	
    public static <T> void sadd(String key,Collection<String> collection){
		ShardedJedisPool pool = cache.getShardedJedisPool();
		ShardedJedis shardJedis = null;	
		Jedis jedis = null;	 
		try {
			shardJedis = pool.getResource();
			jedis = shardJedis.getShard(key);
			if (jedis != null) {
				String[] rsList = new String[collection.size()];
				int i=0;
				for(String t : collection){
					rsList[i++] = t;
				}
				jedis.sadd(key, rsList);
			}
		} catch (Exception e) {
			if (shardJedis != null) pool.returnBrokenResource(shardJedis);
			log.error("sadd %s error!",key, e);
		}finally{
			if (shardJedis != null) pool.returnResource(shardJedis);
		}
	}
    

	 public static void sCookieAdd(String key,Collection<String> collection){
		 ShardedJedis shardJedis = null;	
		 ShardedJedisPool pool = cookie_redis.getShardedJedisPool();
			try {
				Jedis jedis = null;	 
				shardJedis = pool.getResource();
				jedis = shardJedis.getShard(key);
				if (jedis != null) {
					String[] rsList = new String[collection.size()];
					int i=0;
					for(String t : collection){
						rsList[i++] = t;
					}
					jedis.sadd(key, rsList);
				}
			} catch (Exception e) {
				if (shardJedis != null) pool.returnBrokenResource(shardJedis);
				log.error("sadd %s error!",key, e);
			}finally{
				if (shardJedis != null) pool.returnResource(shardJedis);
			}
		}
}
